from django.db import models
from django.conf import settings
import os

# Create your models here.


class Project(models.Model):
    '''项目表'''
    name = models.CharField(verbose_name='项目名',max_length=64,unique=True)
    description = models.CharField(verbose_name='项目描述',max_length=64,blank=True,null=True)
    git_address = models.CharField(verbose_name='Git地址', max_length=100)
    auth = models.ForeignKey(to='Auth',on_delete=models.CASCADE)
    build_command = models.TextField(verbose_name='打包命令',blank=True,null=True,default='')

    @property
    def workspace(self):
        '''项目工作目录'''
        dir = os.path.join(settings.WORK_SPACE,self.name)
        return dir

    @property
    def repo_dir(self):
        '''Git目录'''
        dir = os.path.join(settings.WORK_SPACE,self.name,'repo')
        return dir

    class Meta:
        verbose_name_plural = 'Git项目'

    def __str__(self):
        return self.name


class Auth(models.Model):
    '''Git账号'''
    name = models.CharField(max_length=64)
    password = models.CharField(max_length=64)

    class Meta:
        unique_together = ('name', 'password',)

    def __str__(self):
        return '<%s: %s>'%(self.name,self.password[:4]+'****')


class Branch(models.Model):
    '''Git分支'''
    branch = models.CharField(max_length=64)
    description = models.CharField(max_length=64,blank=True,null=True)
    project = models.ForeignKey(to='Project',on_delete=models.CASCADE)

    def __str__(self):
        return self.branch


class SubModul(models.Model):
    '''项目的子模块'''
    name = models.CharField(max_length=64,verbose_name='模块名称')
    order = models.IntegerField(default=1,verbose_name='更新顺序')
    description = models.CharField(max_length=64, blank=True, null=True,verbose_name='模块描述')
    source_file= models.CharField(max_length=255,verbose_name='打包后生成的文件的路径')
    target_file = models.CharField(max_length=255, verbose_name='服务器上的文件路径')
    backup_command = models.TextField(max_length=255,verbose_name='备份命令',blank=True,null=True)
    stop_command = models.TextField(max_length=255, verbose_name='停止服务命令',blank=True,null=True)
    start_command = models.TextField(max_length=255, verbose_name='启动服务命令',blank=True,null=True)
    clean_old_file_command = models.TextField(max_length=255, verbose_name='清理旧文件命令',blank=True,null=True)
    project = models.ForeignKey(to='Project', on_delete=models.CASCADE, verbose_name='所属项目')

    class Meta:
        unique_together = ('name', 'project')

    def __str__(self):
        return self.name


class Configuration(models.Model):
    '''配置文件'''
    file_path = models.CharField(max_length=255,verbose_name='配置文件路径')
    file_type = models.CharField(max_length=32, verbose_name='配置文件类型',help_text='如:java,xml,yml')
    description = models.CharField(max_length=64, blank=True, null=True, verbose_name='配置文件描述')
    config = models.TextField(verbose_name='配置文件内容')
    submodul = models.ForeignKey(to='SubModul',on_delete=models.CASCADE)

    def __str__(self):
        return self.file_path

    class Meta:
        unique_together = ('file_path', 'submodul')


class Host(models.Model):
    '''服务器表'''
    ip_address = models.GenericIPAddressField(verbose_name='IP地址',unique=True)
    ssh_port = models.IntegerField(verbose_name='SSH端口',default='22')
    ssh_user = models.CharField(max_length=32, verbose_name='SSH用户',default='root')
    ssh_password = models.CharField(max_length=32, verbose_name='SSH密码')
    description = models.CharField(max_length=64, blank=True, null=True,verbose_name='服务器描述')

    def __str__(self):
        return '%s:  %s'%(self.ip_address,self.description)


class SubModul2Host(models.Model):
    '''子模块——主机多对多关联表'''
    submodul = models.ForeignKey(to='SubModul',on_delete=models.CASCADE)
    host = models.ForeignKey(to='Host', on_delete=models.CASCADE)

    is_customize = models.BooleanField(max_length=16,verbose_name='是否单独配置',default=False)
    source_file= models.CharField(max_length=255,verbose_name='打包后生成的文件的路径')
    target_file = models.CharField(max_length=255, verbose_name='服务器上的文件路径')
    backup_command = models.TextField(max_length=255,verbose_name='备份命令',blank=True,null=True)
    stop_command = models.TextField(max_length=255, verbose_name='停止服务命令',blank=True,null=True)
    start_command = models.TextField(max_length=255, verbose_name='启动服务命令',blank=True,null=True)
    clean_old_file_command = models.TextField(max_length=255, verbose_name='清理旧文件命令',blank=True,null=True)


